package org.jet.emall.rbac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jet.emall.common.constant.CommonMapKey;
import org.jet.emall.common.restful.result.CommonPage;
import org.jet.emall.common.restful.result.CommonResult;
import org.jet.emall.rbac.annotation.ApiInformation;
import org.jet.emall.rbac.dto.AdminLoginLogDTO;
import org.jet.emall.rbac.dto.AdminOpLogDTO;
import org.jet.emall.rbac.service.CustomAdminLogService;
import org.jet.emall.rbac.transfer.AdminLogVoTransfer;
import org.jet.emall.rbac.vo.AdminLoginLogVO;
import org.jet.emall.rbac.vo.AdminOperationLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-20 20:56
 */

@Api(value = "日志Controller",tags = "日志访问接口")
@RestController
public class LogController {


    @Autowired
    private CustomAdminLogService adminLogService;

    @ApiInformation(function = "获取登录日志",menu = "登录日志")
    @ApiOperation(value="获取登录日志")
    @RequestMapping(value = "/log/login",method = RequestMethod.GET)
    public CommonResult<CommonPage<AdminLoginLogVO>> getLoginLogPage(String query, Integer pageNum, Integer pageSize,
                                                                     @RequestAttribute(CommonMapKey.USER_ID_KEY) Integer userId,
                                                                     @RequestAttribute(CommonMapKey.ROLE_ID_KEY) Integer roleId) {
        CommonPage<AdminLoginLogDTO> page = adminLogService.getLoginLogPage(query,pageNum,pageSize,userId,roleId);
        ((CommonPage<AdminLoginLogVO>)(Object)page).setList(AdminLogVoTransfer.INSTANCE.toLoginLogVoList(page.getList()));
        return CommonResult.success((CommonPage<AdminLoginLogVO>)(Object)page,"获取登录日志成功");
    }

    @ApiInformation(function = "获取操作日志",menu = "操作日志")
    @ApiOperation(value="获取操作日志")
    @RequestMapping(value = "/log/operation",method = RequestMethod.GET)
    public CommonResult<CommonPage<AdminOperationLogVO>> getOperationLogPage(String query, Integer pageNum, Integer pageSize,
                                                                             @RequestAttribute(CommonMapKey.USER_ID_KEY) Integer userId,
                                                                             @RequestAttribute(CommonMapKey.ROLE_ID_KEY) Integer roleId) {
        CommonPage<AdminOpLogDTO> page = adminLogService.getOperationLogPage(query,pageNum,pageSize,userId,roleId);
        ((CommonPage<AdminOperationLogVO>)(Object)page).setList( AdminLogVoTransfer.INSTANCE.toOperationLogVoList(page.getList()));
        return CommonResult.success((CommonPage<AdminOperationLogVO>)(Object)page,"获取操作日志成功");
    }
}
